#include "svgviewer.h"
#include "ui_svgviewer.h"

SvgViewer::SvgViewer(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::SvgViewer)
{
    ui->setupUi(this);
    this->setWindowTitle("Svg浏览");
    svgWidget = new SvgWidget(this);
    this->ui->scrollArea->setWidget(svgWidget);
}

SvgViewer::~SvgViewer()
{
    delete ui;
}

void SvgViewer::mousePressEvent(QMouseEvent *e)
{
    mousePressPos = e->pos();
    scrollBarValuesOnMousePress.rx()=ui->scrollArea->horizontalScrollBar()->value();
    scrollBarValuesOnMousePress.ry()=ui->scrollArea->verticalScrollBar()->value();
    e->accept();
}

void SvgViewer::mouseMoveEvent(QMouseEvent *e)
{
    ui->scrollArea->horizontalScrollBar()->setValue(scrollBarValuesOnMousePress.x()-e->pos().x()+mousePressPos.x());
    ui->scrollArea->verticalScrollBar()->setValue(scrollBarValuesOnMousePress.y()-e->pos().y()+mousePressPos.y());
    ui->scrollArea->horizontalScrollBar()->update();
    ui->scrollArea->verticalScrollBar()->update();
    e->accept();
}

#include <QFileDialog>

void SvgViewer::on_pushButton_clicked()
{
    auto diag = new QFileDialog(this);
    diag->setWindowTitle("选择要显示的svg文件");
    diag->setAcceptMode(QFileDialog::AcceptMode::AcceptOpen);
    diag->setNameFilter(tr("svg文件(*.svg)"));
    diag->setDirectory(".");
    if(diag->exec()!= QDialog::Accepted)
    {
        delete diag;
        return;
    }
    auto f = *diag->selectedFiles().begin();
    svgWidget->loadsvg(f.toStdString());
    delete diag;
}
